package com.yf.system.modules.user.task;


import com.yf.ability.face.factory.FaceFactory;
import com.yf.job.service.JobService;
import com.yf.system.modules.user.service.SysUserBindService;
import com.yf.system.modules.user.service.SysUserRoleService;
import lombok.extern.log4j.Log4j2;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 删除用户后、清理用户相关数据
 * @author bool
 */
@Log4j2
@Component
public class CleanUserJob implements Job {

    @Autowired
    private SysUserBindService sysUserBindService;


    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private FaceFactory faceFactory;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {


        JobDetail detail = jobExecutionContext.getJobDetail();
        String name = detail.getKey().getName();
        String group = detail.getKey().getGroup();
        String data = String.valueOf(detail.getJobDataMap().get(JobService.TASK_DATA));

        log.info("++++++++++定时任务：清理已删除的用户数据");
        log.info("++++++++++jobName:{}", name);
        log.info("++++++++++jobGroup:{}", group);
        log.info("++++++++++taskData:{}", data);


        List<String> ids = new ArrayList<>();
        ids.add(data);

        // 移除绑定关系
        sysUserBindService.removeByUserIds(ids);

        // 移除主要关联数据
        sysUserRoleService.removeByUserIds(ids);

        // 移除人脸
        faceFactory.getService().deleteFace(data);

    }



}